package com.example.module_home.netalert

import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.util.Log

class NetworkListenerImpl : ConnectivityManager.NetworkCallback() {

    val TAG = NetworkListenerImpl::class.java.simpleName.toString()

    override fun onAvailable(network: Network) {
        super.onAvailable(network)
        Log.e(TAG, "==网络可用")
    }

    override fun onUnavailable() {
        super.onUnavailable()

    }

    override fun onLosing(network: Network, maxMsToLive: Int) {
        super.onLosing(network, maxMsToLive)
        Log.e(TAG, "==网络信号弱")
    }

    override fun onLost(network: Network) {
        super.onLost(network)
        Log.e(TAG, "==网络已断开")
        val test_invoke = ::testInvoke  //加双冒号使一个函数变为函数类型，函数类型才有.invoke()方法
        test_invoke.invoke(1)
        //testInvoke.invoke(1)
    }


    override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
        super.onCapabilitiesChanged(network, networkCapabilities)

        if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) { //标识成功识别到网络
            when {
                networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> Log.e(
                    TAG,
                    "==wifi网络"
                )
                networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> Log.e(
                    TAG,
                    "===移动蜂窝网"
                )

                else -> Log.e(TAG, "==其他网络")
            }
        }
    }


    fun testInvoke(age: Int) {}

}